<?php
/**
 * @version		$Id: controller.php 14401 2010-01-26 14:10:00Z louis $
 * @package		Joomla
 * @subpackage	Search
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant to the
 * GNU General Public License, and as distributed it includes or is derivative
 * of works licensed under the GNU General Public License or other free or open
 * source software licenses. See COPYRIGHT.php for copyright notices and
 * details.
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport('joomla.application.component.controller');

/**
 * @package		Joomla
 * @subpackage	Mwc_Employee
 */
class Mwc_EmployeeController extends JController{
    function __construct( $config = array() ){
        parent::__construct( $config );
        
        // Register Extra tasks
        $this->registerTask( 'add',          'edit' );
        //$this->registerTask( 'edit',         'edit' );
        //$this->registerTask( 'save',         'save' );
        $this->registerTask( 'apply',        'save' );
        //$this->registerTask( 'unpublish',    'publish' );
    }
    
	/**
	 * Show list
	 */
	function display(){
		$model	=& $this->getModel( 'Mwc_Employee' );
		$view   =& $this->getView( 'Default' );
		$view->setModel( $model, true ); 
		$view->display();
	}
    
    function edit(){
        $model  =& $this->getModel( 'Mwc_Employee' );
        $view   =& $this->getView( 'Default' );
        $view->setModel( $model, true ); 
        $view->edit();        
    }
    
    function save(){     
        global $mainframe, $option; 
              
        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );
         
        // save the poll parent information
        $model =& $this->getModel( 'Mwc_Employee' );          
        $row = $model->getTable('mwc_employeedep', 'Table');     
        $post = JRequest::get('post');
        if(!$row->bind( $post )){
            JError::raiseError(500, $row->getError());
        }
        $isNew = ($row->id == 0);
        
        if (!$row->check()){
            JError::raiseError(500, $row->getError());
        }   
        if(!$row->store()){
            JError::raiseError(500, $row->getError() );
        }
        $row->checkin();
       
        switch ($this->_task){
            case 'apply':
                $msg = JText::_( 'Changes to Department saved' );
                $link = "index.php?option=$option&task=edit&cid[]={$row->id}";
                break;

            case 'save':
            default:
                $msg = JText::_( 'Department saved' );
                $link = "index.php?option=$option";
                break;
        }

        $this->setRedirect($link, $msg);
    }
    
    function cancel(){
        global $mainframe, $option; 
        
        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );

        $id  = JRequest::getVar( 'id', 0, '', 'int' );        
        $model =& $this->getModel( 'Mwc_Employee' );          
        $row = $model->getTable('mwc_employeedep', 'Table');

        $row->checkin( $id );
        $this->setRedirect( "index.php?option=$option" );
    }
    
    function publish(){
        global $mainframe, $option;

        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );

        $db         =& JFactory::getDBO();
        $user       =& JFactory::getUser();   
        $cid        = JRequest::getVar( 'cid', array(), '', 'array' );
        $publish    = ( $this->getTask() == 'publish' ? 1 : 0 );

        JArrayHelper::toInteger($cid); 
        if (count( $cid ) < 1){
            $action = $publish ? 'publish' : 'unpublish';
            JError::raiseError(500, JText::_( 'Select an item to' .$action, true ) );
        }

        $cids = implode( ',', $cid );

        $query = 'UPDATE #__mwc_employee_dep'
        . ' SET published = ' . (int) $publish
        . ' WHERE id IN ( '. $cids .' )'
        . ' AND ( checked_out = 0 OR ( checked_out = '.(int) $user->get('id').' ) )'
        ;
        $db->setQuery( $query );
        if (!$db->query()){
            JError::raiseError(500, $db->getErrorMsg() );
        }

        if (count( $cid ) == 1){
            $model =& $this->getModel( 'Mwc_Employee' );          
            $row = $model->getTable('mwc_employeedep', 'Table');
            $row->checkin( $cid[0] );
        }
        
        $this->setRedirect( "index.php?option=$option" );
        //$mainframe->redirect( "index.php?option=$option" );
    }
    
    function remove(){
        global $mainframe, $option;
        
        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );

        $db     =& JFactory::getDBO();
        $cid    = JRequest::getVar( 'cid', array(), '', 'array' );

        JArrayHelper::toInteger($cid);
        $msg = 'Item(s) deleted';
        $model =& $this->getModel( 'Mwc_Employee' );
        
        for ($i = 0, $n = count($cid); $i < $n; $i++){                       
            $row = $model->getTable('mwc_employee', 'Table');
            if(!$row->delete( $cid[$i])){
                $msg .= $row->getError();
            }
        }
        
        $this->setRedirect( "index.php?option=$option", $msg );
    }
}
